{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n",
      "{'bobby', 'bobby2', 'imooc'}\n"
     ]
    }
   ],
   "source": [
    "## 列表推导式（列表生成式）\n",
    "## 1、提取出 1 - 20 之间的奇数\n",
    "\n",
    "odd_list = []\n",
    "for i in range(21):\n",
    "    if i % 2 == 1:\n",
    "        odd_list.append(i)\n",
    "        \n",
    "odd_list = [i for i in range(21) if i % 2 == 1]\n",
    "\n",
    "## 2、逻辑复杂的情况\n",
    "def handle_item(item):\n",
    "    return item * item\n",
    "odd_list = [handle_item(i) for i in range(21) if i % 2 == 1]\n",
    "\n",
    "## 列表生成式性能高于列表操作\n",
    "\n",
    "# print(type(odd_list))\n",
    "# print(odd_list)\n",
    "\n",
    "###############################################\n",
    "## 生成器表达式，（小括号）\n",
    "odd_list = (i for i in range(21) if i % 2 == 1)\n",
    "# print(type(odd_list))\n",
    "# print(odd_list)\n",
    "\n",
    "###############################################\n",
    "## 字典推导式\n",
    "my_dict = {'bobby': 22, 'bobby2':23, 'imooc': 5}\n",
    "reversed_dict = {value: key for key, value in my_dict.items()}\n",
    "# print(reversed_dict)\n",
    "\n",
    "###############################################\n",
    "## 集合推导式\n",
    "my_set = {key for key, value in my_dict.items()}\n",
    "# my_set = set(my_dict.keys())\n",
    "print(type(my_set))\n",
    "print(my_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
